package com.qf.springbootproject.consumer;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.springbootproject.es.entity.ProductParamsEsVo;
import com.qf.springbootproject.es.repository.ProductParamsEsVoRepository;
import com.qf.springbootproject.product.entity.ProductParams;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

@Component
public class Consumer {
    @Autowired
    ProductParamsEsVoRepository productParamsEsVoRepository;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @RabbitListener(queues = "product")
    public void getMsg(String product, Channel channel, Message message) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        //ProductParamsEsVo productParamsEsVo = objectMapper.readValue(product, ProductParamsEsVo.class);
        ProductParamsEsVo productParamsEsVo = JSONUtil.toBean(product, ProductParamsEsVo.class);
        elasticsearchRestTemplate.createIndex(ProductParamsEsVo.class);
        elasticsearchRestTemplate.putMapping(ProductParamsEsVo.class);
        productParamsEsVoRepository.save(productParamsEsVo);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}

